<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Middleware\JWTAuthMiddleware;
use App\Services\GroupService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([JWTAuthMiddleware::class])]
#[Controller(prefix: '/api/v1/group/')]
class GroupController extends CController
{
    #[Inject]
    protected GroupService $groupService;

    /**
     * 群聊列表.
     */
    #[RequestMapping(path: 'list', methods: 'GET')]
    public function list()
    {
        $params = $this->request->all();
        $params['user'] = auth()->user();
        $data = $this->groupService->list($params);
        return $this->success($data, $this->request);
    }

    /**
     * 创建群聊或邀请好友进群.
     */
    #[RequestMapping(path: 'action', methods: 'POST')]
    public function action()
    {
        $params = $this->request->all();
        $params['user'] = auth()->user();
        $this->validate($params, [
            'group_users' => 'required|array',
            'action' => 'required|string',
        ]);
        $data = $this->groupService->action($params);
        return $this->success($data, $this->request);
    }
}
